<script lang="ts" setup>
import BaseButton from "@/components/BaseButton/index.vue";
import BaseUpDown from "@/components/BaseUpDown/index.vue";
import useSearch from "@/hooks/useSearch";
import useData from "./hooks/useData";
import { useUserStore } from "@/stores/modules/user";
import { formatDate } from "@/utils";

const { pageData, agrMap } = useData();
import { deleteData, getDataByPage } from "@/api/modules/control-strategy/access-control";
import UpdateForm from "./components/UpdateForm/index.vue";

const userStore = useUserStore();
const username = computed(() => userStore.userInfo.name);
const { tableData, loadData, formData, reloadData } = useSearch({
  defaultData: {
    ipproto: "0"
  },
  responseKey: {
    list: "data",
    total: "total"
  }
});

function loadDataCb() {
  loadData({
    api: getDataByPage,
    sendData: {
      pageNum: formData.pageNum - 1,
      pageSize: formData.pageSize,
      sip: formData.sip,
      sport: formData.sport,
      dip: formData.dip,
      dport: formData.dport,
      ipproto: formData.ipproto
    }
  });
}

loadDataCb();

function reloadDataCb() {
  reloadData();
  loadData({
    api: getDataByPage,
    sendData: {
      pageNum: formData.pageNum - 1,
      pageSize: formData.pageSize,
      sip: formData.sip,
      sport: formData.sport,
      dip: formData.dip,
      dport: formData.dport,
      ipproto: formData.ipproto
    }
  });
}

loadDataCb();

/**删除*/
import useDelete from "@/hooks/useDelete";
import { computed } from "vue";
import BaseCollapse from "@/components/BaseCollapse/index.vue";

const { deleteHandler, idList } = useDelete();

async function deleteCb(id) {
  await deleteHandler({
    api: deleteData,
    multiple: false,
    sendData: {
      id: [id]
    },
    hideMessageBox: false
  });
  loadDataCb();
}

function selectCb(data) {
  console.log(data);
  idList.value = data.rowList.map(item => item.userId);
}
</script>

<template>
  <BaseUpDown>
    <template #up>
      <BaseCollapse title="访问控制">
        <template #header>
          <div class="h-box justify-end" style="width: 150px;float: right" @click.stop>
            <UpdateForm @success="loadDataCb()">
              <BaseButton type="add_icon_text" />
            </UpdateForm>
            <BaseButton @click="reloadDataCb()" type="refresh_icon_text" />
          </div>
        </template>
        <el-form>
          <div class="h-box" style="flex-wrap: wrap">
            <el-form-item label="源IP">
              <el-input v-model="formData.sip" placeholder="请输入" style="width: 120px" />
            </el-form-item>
            <el-form-item class="mg-l-sm" label="源端口">
              <el-input v-model="formData.sport" placeholder="请输入" style="width: 120px" />
            </el-form-item>
            <el-form-item class="mg-l-sm" label="协议">
              <el-select v-model="formData.ipproto" style="width: 120px">
                <el-option value="0" label="所有"></el-option>
                <el-option value="6" label="TCP"></el-option>
                <el-option value="17" label="UDP"></el-option>
                <el-option value="1" label="ICMP"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="mg-l-sm" label="目的IP">
              <el-input v-model="formData.dip" placeholder="请输入" style="width: 120px" />
            </el-form-item>
            <el-form-item class="mg-l-sm" label="目的端口">
              <el-input v-model="formData.dport" placeholder="请输入" style="width: 120px" />
            </el-form-item>
            <BaseButton class="mg-l-sm" style="margin-bottom: 18px" @click="loadDataCb()" type="search_icon_text" />
            <BaseButton class="mg-l-sm" style="margin-bottom: 18px" @click="reloadDataCb()" type="reset_icon_text" />
          </div>
        </el-form>
      </BaseCollapse>

    </template>
    <he-table
      @handle-current-change="loadDataCb"
      :total="tableData.total"
      v-model:page-size="formData.pageSize"
      v-model:page-num="formData.pageNum"
      operate-width="180px"
      :operate-list="[]"
      :table-data="tableData.list"
      :column-data="pageData.columnData"
      :show-multiple="false"
      :show-summary="false"
      :show-operate="false"
      :show-multiple-column="false"
      :show-pagination="true"
      @select="selectCb"
    >
      <template #0Default="{ data }">
        {{ data.$index + 1 }}
      </template>
      <template #8Default="{ data }">
        {{ formatDate(data.row[8]) }}
      </template>
      <template #4Default="{ data }">
        {{ agrMap.get(data.row[4]) }}
      </template>
      <template #operationDefault="{ data }">
        <UpdateForm
          :row-data="{
            id: data.row[0],
            name: data.row[1],
            state: 1,
            ipproto: data.row[4],
            sip: data.row[2],
            dip: data.row[3],
            sport: data.row[5],
            dport: data.row[6]
          }"
          :title="`编辑`"
          @success="loadDataCb"
        >
          <base-button type="edit_text"></base-button>
        </UpdateForm>
        <base-button @click="deleteCb(data.row[0])" type="del_text"></base-button>
      </template>
    </he-table>
  </BaseUpDown>
</template>
